package com.hbzhit.common.entity;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.hbzhit.common.utils.CacheUtils;
import com.hbzhit.common.utils.Constant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.Date;

/**
 * 实体类基类
 * 定义各个对象的通用字段
 * @author DING
 */
@ApiModel("基础实体类")
@Slf4j
@Getter
@Setter
@TableName("base")
public class BaseEntity implements Serializable {
    @ApiModelProperty("ID")
    @TableId
    private Long id;

    @ApiModelProperty("组织id")
    @TableField(fill = FieldFill.INSERT)
    private Long orgId;

    @ApiModelProperty("创建人ID")
    @TableField(fill = FieldFill.INSERT)
    private Long creator;

    @ApiModelProperty("创建时间")
    @TableField(fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    @ApiModelProperty("编辑人ID")
    @TableField(fill = FieldFill.UPDATE)
    private Long modifier;

    @ApiModelProperty("编辑时间")
    @TableField(fill = FieldFill.UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date modifyTime;

    @ApiModelProperty("删除标识")
    @TableField(fill = FieldFill.INSERT)
    @TableLogic
    private Integer isDel;

    @ApiModelProperty("组织名称")
    @Excel(name = "所属单位", width = 30)
    @TableField(exist = false)
    private String orgName;

    @ApiModelProperty("组织编码")
    @TableField(exist = false)
    private String orgCode;

    @ApiModelProperty("创建人名称")
    @TableField(exist = false)
    private String creatorName;

    @ApiModelProperty("编辑人名称")
    @TableField(exist = false)
    private String modifierName;

    public String getOrgName() {
        return ObjectUtils.isNotEmpty(this.orgId) ? CacheUtils.getOrgCache(this.orgId).getOrgName() : this.orgName;
    }

    public String getOrgCode() {
        return ObjectUtils.isNotEmpty(this.orgId) ? CacheUtils.getOrgCache(this.orgId).getOrgCode() : this.orgCode;
    }

    public String getCreatorName() {
        return ObjectUtils.isNotEmpty(this.creator) ? CacheUtils.getUserCache(this.creator).getRealName() : this.creatorName;
    }

    public String getModifierName() {
        return ObjectUtils.isNotEmpty(this.modifier) ? CacheUtils.getUserCache(this.modifier).getRealName() : this.modifierName;
    }

    public Integer getIsDel() {
        return isDel == null ? Constant.INT_N : isDel;
    }
}
